import { defineConfig,loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
//vue
import Components from 'unplugin-vue-components/vite'
import { VantResolver } from 'unplugin-vue-components/resolvers'

//nutui
import { createStyleImportPlugin } from 'vite-plugin-style-import'

//别名
import { resolve } from 'path'

// https://vitejs.dev/config/
//配置Axios需要将({command, mode})箭头函数 =>加在中间
export default ({command, mode}) =>  defineConfig({
  plugins: [vue(),
  //vue
   Components({
        resolvers: [VantResolver()],
      }),
	  // NutUI
		createStyleImportPlugin({
		      resolves: [{
		          libraryName: '@nutui/nutui',
		          libraryNameChangeCase: 'pascalCase',
		          resolveStyle: (name) => {
		            return `@nutui/nutui/dist/packages/${name.toLowerCase()}/index.scss`
		          },
		      }]
		}),
	 
  ],
  
  // add here  NutUI
    css: {
      preprocessorOptions: {
        scss: {
          // 配置 nutui 全局 scss 变量
          additionalData: `@import "@nutui/nutui/dist/styles/variables.scss";`
        }
      }
    },
  
  //别名
  resolve: {
       alias: [
		   //@ =》 表示 vue-vite-trmplate/src
  		
		{ find: '@', replacement: resolve(__dirname, 'src') },
  		//@ =》 表示 vue-vite-trmplate/src/components
		{ find: '_c', replacement: resolve(__dirname, 'src/components') },
  		{ find: '_v', replacement: resolve(__dirname, 'src/views') },
  	]
    },
	//配置Axios
		server: {
			// host: true,
			// hmr: true,
			proxy: {
				'/api': {
					// 根据不同运行环境加载不同的配置文件
					target: loadEnv(mode, process.cwd()).VITE_API_TARGET_URL,
					// 根据配置文件加载
					// target: serverConfig.server.dev,
					changeOrigin: true,	//是否要跨域
					rewrite: (path) => path.replace(/^\/api/, '')	//proxy转发，会去调前缀/qpi
					// rewrite 会帮我重写，如果不想重写，可以直接去掉这一部分
				}
			}
		},
})
